<?php
/**
 * File		: ~page.php
 * Project	: Protected Agriculture 
 * Author 	: Norma1
 * E-mail 	: JYDSliu@Gmail.com
 * Date   	: 2011-03-12
 * Blog   	: blog.jyds.org
 */
class page {
	private $filename = '';
	private $params = '';
	private $page_start = '';
	private $page_size = '';
	private $page_number = '';
	private $initfirst_page = '';
	private $first_page = '';
	private $last_page = '';
	private $prev_page = '';
	private $next_page = '';
	private $prev_pages = '';
	private $next_pages = '';
	private $totallines = '';
	private $totalpages = '';
	private $getpage = '';
	private $param = '';
	private $paramlist = '';
	function __construct() {
		$this->set_initfirstpage ( "仅此一页" );
		$this->set_pagestart ( 1 );
		$this->set_setpage ( 1 );
		$this->set_pagesize ( 10 );
		$this->set_params ( "" );
		$this->set_pagenumber ( 5 );
		$this->set_firstpage ( '首  页' );
		$this->set_prevpage ( '上一页' );
		$this->set_nextpage ( '下一页' );
		$this->set_prevpages ( '上' . $this->page_number . '页' );
		$this->set_nextpages ( '下' . $this->page_number . '页' );
		$this->set_lastpage ( '末  页' );
		$this->set_filename ( basename ( $_SERVER ['PHP_SELF'] ) );
	}
	protected function _style1($name) {
		return '<div class="spile-page1" onmouseover="this.className=\'spile-page2\';" onmouseout="this.className=\'spile-page1\';">' . $name . '</div>';
	}
	protected function _style2($name) {
		return '<div class="spile-page3" onmouseover="this.className=\'spile-page4\';" onmouseout="this.className=\'spile-page3\';">' . $name . '</div>';
	}
	protected function _style3($name) {
		return '<div class="spile-page4">' . $name . '</div>';
	}
	protected function _style4($name) {
		return '<div class="spile-page1-desable">' . $name . '</div>';
	}
	function set_pagesize($pagesize) {
		$this->page_size = $pagesize;
	}
	function set_filename($filename) {
		$this->filename = $filename;
	}
	function set_params($params) {
		$this->params = $params;
	}
	function set_pagestart($pagestart) {
		$this->page_start = $pagestart;
	}
	function set_pagenumber($pagenumber) {
		$this->page_number = $pagenumber;
	}
	function set_firstpage($firstpage) {
		$this->first_page = $firstpage;
	}
	function set_lastpage($lastpage) {
		$this->last_page = $lastpage;
	}
	function set_prevpage($prevpage) {
		$this->prev_page = $prevpage;
	}
	function set_nextpage($nextpage) {
		$this->next_page = $nextpage;
	}
	function set_prevpages($prevpages) {
		$this->prev_pages = $prevpages;
	}
	function set_nextpages($nextpages) {
		$this->next_pages = $nextpages;
	}
	function set_initfirstpage($initfirstpage) {
		$this->initfirst_page = $initfirstpage;
	}
	function set_setpage($getpage) {
		$this->getpage = $getpage;
	}
	function get_startpage() {
		return $this->page_start;
	}
	function get_maxpage() {
		return ($this->totalpages - $this->page_start + 1);
	}
	function get_result($sql) {
		$this->totallines = choice_count ( $sql );
		$this->totalpages = empty ( $this->totallines ) ? 1 : ceil ( $this->totallines / $this->page_size );
		$start = ($this->getpage - 1) * $this->page_size;
		$sql .= ' LIMIT ' . $start . ', ' . $this->page_size;
		$result = choice ( $sql );
		$this->param = preg_replace ( "/&?page=[0-9]*&?/", '', $this->params );
		return $result;
	}
	function showpage($result_type = '') {
		$final = '';
		if ($this->getpage > $this->page_start + intval ( ($this->page_number - 1) / 2 )) {
			$this->paramlist = '?page=' . $this->page_start;
			$this->paramlist .= '&' . $this->param;
			$final .= '<a href=./' . $this->filename . $this->paramlist . ' title="首页" >' . $this->_style1 ( $this->first_page ) . '</a>&nbsp;';
		} else
			$final .= $this->_style4 ( $this->first_page ) . '&nbsp;';
		if ($this->getpage - $this->page_number > $this->page_start - 1) {
			$this->paramlist = '?page=' . ($this->getpage - $this->page_number);
			$this->paramlist .= '&' . $this->param;
			$final .= '<a href=./' . $this->filename . $this->paramlist . ' title="上' . $this->page_number . '页" >' . $this->_style1 ( $this->prev_pages ) . '</a>&nbsp;';
		} else
			$final .= $this->_style4 ( $this->prev_pages ) . '&nbsp;';
		if ($this->getpage > $this->page_start) {
			$this->paramlist = '?page=' . ($this->getpage - 1);
			$this->paramlist .= '&' . $this->param;
			$final .= '<a href=./' . $this->filename . $this->paramlist . ' title="上一页" >' . $this->_style1 ( $this->prev_page ) . '</a>&nbsp;';
		} else
			$final .= $this->_style4 ( $this->prev_page ) . '&nbsp;';
		if ($this->totalpages > $this->page_number) {
			$prev_num = intval ( ($this->page_number - 1) / 2 );
			$next_num = ceil ( ($this->page_number - 1) / 2 );
			if ($this->getpage - $prev_num >= $this->page_start && $this->getpage + $next_num <= $this->totalpages) {
				for($i = ($this->getpage - $prev_num), $this->paramlist = '', $maxpage = $i + $this->page_number; $i < $maxpage; $i ++) {
					$this->paramlist .= '?page=' . $i;
					$this->paramlist .= '&' . $this->param;
					if ($i != $this->getpage) {
						$final .= '<a href=./' . $this->filename . $this->paramlist . '>' . $this->_style2 ( $i ) . '</a>&nbsp;';
					} else {
						$final .= '<strong>' . $this->_style3 ( $i ) . '</strong>&nbsp;';
					}
					$this->paramlist = '';
				}
			} else {
				if ($this->getpage - $prev_num < $this->page_start) {
					for($i = $this->page_start, $this->paramlist = '', $maxpage = $i + $this->page_number; $i < $maxpage; $i ++) {
						$this->paramlist .= '?page=' . $i;
						$this->paramlist .= '&' . $this->param;
						if ($i != $this->getpage) {
							$final .= '<a href=./' . $this->filename . $this->paramlist . '>' . $this->_style2 ( $i ) . '</a>&nbsp;';
						} else {
							$final .= '<strong>' . $this->_style3 ( $i ) . '</strong>&nbsp;';
						}
						$this->paramlist = '';
					}
				}
				if ($this->getpage + $next_num > $this->totalpages) {
					for($i = ($this->totalpages - $this->page_number + 1), $this->paramlist = '', $maxpage = $this->totalpages + 1; $i < $maxpage; $i ++) {
						$this->paramlist .= '?page=' . $i;
						$this->paramlist .= '&' . $this->param;
						if ($i != $this->getpage) {
							$final .= '<a href=./' . $this->filename . $this->paramlist . '>' . $this->_style2 ( $i ) . '</a>&nbsp;';
						} else {
							$final .= '<strong>' . $this->_style3 ( $i ) . '</strong>&nbsp;';
						}
						$this->paramlist = '';
					}
				}
			}
		} else {
			for($i = $this->page_start, $this->paramlist = '', $maxpage = $this->totalpages + 1; $i < $maxpage; $i ++) {
				$this->paramlist .= '?page=' . $i;
				$this->paramlist .= '&' . $this->param;
				if ($i != $this->getpage) {
					$final .= '<a href=./' . $this->filename . $this->paramlist . '>' . $this->_style2 ( $i ) . '</a>&nbsp;';
				} else {
					$final .= '<strong>' . $this->_style3 ( $i ) . '</strong>&nbsp;';
				}
				$this->paramlist = '';
			}
		}
		if ($this->getpage < $this->totalpages) {
			$this->paramlist = '?page=' . ($this->getpage + 1);
			$this->paramlist .= '&' . $this->param;
			$final .= '<a href=./' . $this->filename . $this->paramlist . ' title="下一页" >' . $this->_style1 ( $this->next_page ) . '</a>&nbsp;';
		} else
			$final .= $this->_style4 ( $this->next_page ) . '&nbsp;';
		if (($this->getpage + $this->page_number) < $this->totalpages + 1) {
			$this->paramlist = '?page=' . ($this->getpage + $this->page_number);
			$this->paramlist .= '&' . $this->param;
			$final .= ' <a href=./' . $this->filename . $this->paramlist . ' title="下' . $this->page_number . '页" >' . $this->_style1 ( $this->next_pages ) . '</a>&nbsp;';
		} else
			$final .= $this->_style4 ( $this->next_pages ) . '&nbsp;';
		if ($this->getpage < $this->totalpages - ceil ( ($this->page_number + 1) / 2 )) {
			$this->paramlist = '?page=' . $this->totalpages;
			$this->paramlist .= '&' . $this->param;
			$final .= ' <a href=./' . $this->filename . $this->paramlist . ' title="末页" >' . $this->_style1 ( $this->last_page ) . '</a>';
		} else
			$final .= $this->_style4 ( $this->last_page ) . '&nbsp;';
		if ($this->totalpages == 1)
			$final = $this->_style4 ( $this->initfirst_page );
		if ($result_type == 1) {
			return $final;
		} else {
			echo $final;
		}
	}
	function jumptopage($result_type = '') {
		$final = "转到第 <select name=\"page\" size=\"1\" onchange=\"javascript:location.href='./" . $this->filename . "?page='+this.options[selectedIndex].value+'&" . $this->param . "';\">";
		for($i = $this->page_start; $i <= $this->totalpages; $i ++) {
			$final .= '<option value="' . $i . '" ';
			if ($i == $this->getpage) {
				$final .= 'selected="selected">' . $i . '</option>';
			} else {
				$final .= '>' . $i . '</option>';
			}
		}
		$final .= '</select> 页';
		if ($result_type == 1) {
			return $final;
		} else {
			echo $final;
		}
	}
}
?>